package tritop.android.naturalselectionnews;



import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class PicGallery extends Activity {
	private Gallery mGallery;
	private ImageView mImgView;
	private Intent srvIntent;
	private AlertDialog.Builder mABuilder;
	private ImageAdapter mImageAdapter;
	
	OnItemClickListener galleryItemClicked= new OnItemClickListener(){
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			mImgView.setImageResource((Integer)mGallery.getAdapter().getItem(position));
		}
    };
	
	
    OnClickListener previewClicked= new OnClickListener(){

		@Override
		public void onClick(View v) {
			PicGallery.this.showWpDialog();
		}
    	
    };
    
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.imagegallery);
        mGallery = (Gallery) this.findViewById(R.id.galleryThumbnails);
        mImgView = (ImageView) this.findViewById(R.id.imageViewPreview);
        mABuilder = new AlertDialog.Builder(this);
        mImageAdapter = new ImageAdapter(this);
		
        Toast.makeText(this, "Push the big preview image to set your new wallpaper", Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onResume() {
		super.onResume();
	    mGallery.setAdapter(mImageAdapter);
	    mGallery.setOnItemClickListener(galleryItemClicked);
		mImgView.setImageResource((Integer)mGallery.getAdapter().getItem(0));
		mImgView.setOnClickListener(previewClicked);
		
	}
	
	
	@Override
	protected void onDestroy() {
		//fix for issue 8488 bitmap > VM size budget
		for(int i=0;i<mGallery.getChildCount();i++){
			ImageView imgv=(ImageView)mGallery.getChildAt(i);
			BitmapDrawable bmdraw=(BitmapDrawable)imgv.getDrawable();
			bmdraw.setCallback(null);
			bmdraw.getBitmap().recycle();		
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		//System.gc();
	}
	
	
	public void showWpDialog(){
		mABuilder.setMessage(getString(R.string.picGallery_wallpaper_question))
	       .setCancelable(false)
	       .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	               setWallpaper();
	           }
	       })
	       .setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	                dialog.cancel();
	           }
	       });
		AlertDialog alertDialog = mABuilder.create();
		alertDialog.show();
	}
	
	
	private void setWallpaper(){
		BitmapDrawable bDraw= (BitmapDrawable)mImgView.getDrawable();
		WallpaperManager wMgr = WallpaperManager.getInstance(this);
		try {
			wMgr.setBitmap(bDraw.getBitmap());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	

}
